package com.etc.patient.service.impl;

import cn.hutool.json.JSONUtil;
import com.etc.common.entity.MedicalRecord;
import com.etc.common.mapper.LogMapper;
import com.etc.common.utils.Result;
import com.etc.common.utils.UUIDUtil;
import com.etc.common.entity.BasicPatient;
import com.etc.patient.mapper.BasicPatientMapper;
import com.etc.patient.mapper.MedicalRecordMapper;
import com.etc.common.mapper.OutpatientServiceMapper;
import com.etc.patient.service.BasicPatientService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.etc.common.utils.RedisConstants.CACHE_MEDICALRECORD;
import static com.etc.common.utils.RedisConstants.CACHE_PATIENT;


@Service
public class BasicPatientServiceImpl implements BasicPatientService {
    @Resource
    private BasicPatientMapper basicPatientMapper;
    @Resource
    private MedicalRecordMapper mrMapper;

    @Resource
    private OutpatientServiceMapper osMapper;
    @Resource
    private LogMapper logMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询医生Id下的所有病人信息列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result queryDocId(Integer pageNum, Integer pageSize, String docId) {
        //获取是否有值
        Boolean isExist = stringRedisTemplate.hasKey(CACHE_PATIENT + ":docId:" + docId);
        if (!isExist) {
            //没有数据，则查询数据库
            List<BasicPatient> patientList = basicPatientMapper.queryByDocId(docId);
            for (BasicPatient patient : patientList) {
                //转化成json
                String jsonStr = JSONUtil.toJsonStr(patient);
                //写入redis
                stringRedisTemplate.opsForList().leftPush(CACHE_PATIENT + ":docId:" + docId, jsonStr);
            }
        }
        //有值，从redis中获取医生数据
        List<String> result = stringRedisTemplate.opsForList().range(CACHE_PATIENT + ":docId:" + docId, pageSize * (pageNum - 1), pageSize * (pageNum - 1) + pageSize - 1);
        //转化成list
        List<BasicPatient> list = JSONUtil.toList(result.toString(), BasicPatient.class);
        return Result.ok(list, (long) list.size());
    }


    /**
     * 新增病人信息
     *
     * @param basicPatient
     * @return
     */
    @Override
    @Transactional
    public Result insert(BasicPatient basicPatient, String operatorId) {
        //根据身份证查询数据库是否存在该病人
        BasicPatient patient = basicPatientMapper.queryByIdCard(basicPatient.getPatientIdCard());
        if (patient != null) {
            //病人存在，则更新病人数据
            basicPatient.setPatientId(patient.getPatientId());
            basicPatientMapper.updateById(basicPatient);
            logMapper.insert(UUIDUtil.getUUID(), operatorId,
                    "新增时修改病人" + basicPatient.getPatientName() + "的信息，ID为：" + basicPatient.getPatientId());
        } else {
            //生成病人ID
            basicPatient.setPatientId(UUIDUtil.getUUID());
            //新增病人信息
            basicPatientMapper.insert(basicPatient);
            //新增病人操作日志
            logMapper.insert(UUIDUtil.getUUID(), operatorId,
                    "新增病人:" + basicPatient.getPatientName());
        }
        //删除redis中的病人数据
        stringRedisTemplate.delete(CACHE_PATIENT + ":docId:" + operatorId);
        return Result.ok(1, "新增成功");
    }

    /**
     * 修改病人
     *
     * @param basicPatient
     * @return
     */
    @Override
    @Transactional
    public Result updateById(BasicPatient basicPatient, String operatorId) {
        basicPatientMapper.updateById(basicPatient);
        logMapper.insert(UUIDUtil.getUUID(), operatorId,
                "修改病人" + basicPatient.getPatientName() + "的信息，ID为：" + basicPatient.getPatientId());
        //删除redis中的病人数据
        stringRedisTemplate.delete(CACHE_PATIENT + ":docId:" + operatorId);
        return Result.ok(1, "修改成功");
    }

    /**
     * 批量结束病人治疗
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result endPatient(String[] ids, String operatorId) {
        //删除redis中的病人数据
        stringRedisTemplate.delete(CACHE_PATIENT + ":docId:" + operatorId);
        //删除redis中的病历数据
        stringRedisTemplate.delete(CACHE_MEDICALRECORD + ":docId:" + operatorId);
        for (String id : ids) {
            //对病人状态信息进行批量修改
            basicPatientMapper.updateStatus(id, 0);
            //for循环 新增病人操作日志
            logMapper.insert(UUIDUtil.getUUID(), operatorId, "结束病人治疗:" + id);
            //查询最近的门诊数据，并获取门诊Id
            String osId = osMapper.selectOsId(id);
            //生成未归档的电子病历信息
            MedicalRecord medicalRecord = new MedicalRecord(UUIDUtil.getUUID(), osId, 0);
            mrMapper.insert(medicalRecord);
            //新增病历日志
            logMapper.insert(UUIDUtil.getUUID(), operatorId, "系统生成了关于病人Id为：" + id + "的电子病历。");
        }
        return Result.ok(1, "成功结束治疗");
    }


    /**
     * 根据病人id修改病人状态
     *
     * @param patientId
     * @param status
     * @return
     */
    @Override
    public int updateStatus(String patientId, int status) {
        return basicPatientMapper.updateStatus(patientId, status);
    }
}
